<script setup>
import Panel from './panel.vue';
import { getAgentListAPI } from '@/api/distribution';
import { ref } from 'vue'
import { useInitTable } from '@/composables/useCommon'
import Search from '@/components/Search.vue'
import SearchItem from '@/components/SearchItem.vue'
import DataDrawer from './dataDrawer.vue';

const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  total,
  currentPage,
  getData,
} = useInitTable(
  {
    searchForm: {
      keyword: '',
      type: 'all',
      starttime: null,
      endtime: null,
    },
    getList: getAgentListAPI,
    onGetListSuccess: (res) => {
      tableData.value = res.list
      total.value = res.totalCount
    },
  }
)

const dataDrawerRef = ref(null)
const orderDataDrawerRef = ref(null)
const openDataDrawer = (id, type) => {
  (type === 'order' ? orderDataDrawerRef : dataDrawerRef).value.open(id)
}
</script>
<template>
  <div>
    <Panel></Panel>
    <el-card>
      <!-- 搜索 -->
      <Search :model="searchForm" @search="getData" @reset="resetSearchForm">
        <SearchItem label="时间选择">
          <el-radio-group v-model="searchForm.type">
            <el-radio-button value="all">全部</el-radio-button>
            <el-radio-button value="today">今天</el-radio-button>
            <el-radio-button value="yesterday">昨天</el-radio-button>
            <el-radio-button value="last7days">最近7天</el-radio-button>
          </el-radio-group>

        </SearchItem>
        <template #show>
          <SearchItem label="开始时间">
            <el-date-picker v-model="searchForm.starttime" type="date" placeholder="开始日期" style="width: 90%;"
              value-format="YYYY-MM-DD" />
          </SearchItem>
          <SearchItem label="结束时间">
            <el-date-picker v-model="searchForm.endtime" type="date" placeholder="结束日期" style="width: 90%;"
              value-format="YYYY-MM-DD" />
          </SearchItem>
          <SearchItem label="关键词">
            <el-input v-model="searchForm.keyword" placeholder="关键词" clearable></el-input>
          </SearchItem>
        </template>
      </Search>
      <!-- 表格区域 -->
      <el-table class="mt-3" v-loading="loading" :data="tableData" border stripe>
        <el-table-column label="ID" prop="id" align="center"></el-table-column>
        <el-table-column label="头像" width="65">
          <template #default="scope">
            <!-- 头像 -->
            <el-avatar :size="40" :src="scope.row.avatar">
              <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png" />
            </el-avatar>
          </template>
        </el-table-column>

        <el-table-column label="用户信息" width="200">
          <template #default="{ row }">
            <div class="text-xs">
              <p>用户：{{ row.username }}</p>
              <p>昵称{{ row.nickname }}</p>
              <p>姓名{{ row.user_info.name }}</p>
              <p>电话{{ row.phone }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="推广用户数量" prop="share_num" align="center"></el-table-column>
        <el-table-column label="订单数量" prop="share_order_num" align="center"></el-table-column>
        <el-table-column label="订单金额" prop="order_price" align="center"></el-table-column>
        <el-table-column label="账户佣金" prop="commission" align="center"></el-table-column>
        <el-table-column label="已提现金额" prop="cash_out_price" align="center"></el-table-column>
        <el-table-column label="提现次数" prop="cash_out_time" align="center"></el-table-column>
        <el-table-column label="未提现次数" prop="no_cash_out_price" align="center"></el-table-column>
        <el-table-column fixed="right" width="200" label="操作" align="center">
          <template #default="{ row }">
            <el-button type="primary" size="small" text @click="openDataDrawer(row.id, 'user')">推广人</el-button>
            <el-button type="primary" size="small" text @click="openDataDrawer(row.id, 'order')">推广订单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="flex justify-center mt-auto-3" background layout="prev, pager, next" :total="total"
        :current-page="currentPage" @current-change="getData" />


    </el-card>
    <DataDrawer ref="dataDrawerRef"></DataDrawer>
    <DataDrawer ref="orderDataDrawerRef" type="order"></DataDrawer>
  </div>
</template>